package com.shuqi.activity;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;

import com.shuqi.controller.R;

/**
 * 管理所有Activity 当启动一个Activity时，就将其保存到Stack中， 退出时，从Stack中删除
 * 
 * @author zhangpeng
 * 
 */
public class ActivityManager {

    private static volatile ActivityManager instance;

    private ActivityManager() {}

    /**
     * 创建单例类，提供静态方法调用
     * 
     * @return ActivityManager
     */
    public static ActivityManager getInstance() {
        if (instance == null) {
            instance = new ActivityManager();
        }
        return instance;
    }
 // 离当前页最近的3个activity
    public  ArrayList<WeakReference<Activity>> acts = new ArrayList<WeakReference<Activity>>(
            2);
    /**
     * 书单等零散互调页面维护最新3个的方法
     * @param intent
     * @param oldAct
     */
    public  void startActivity(Intent intent, Activity oldAct) {
        startActivity(intent, oldAct, -1);
    }

    public  void startActivity(Intent intent, Activity oldAct, int requestCode) {

        // 移除被回收的act
        for (int i = 0; i < acts.size(); i++) {
            WeakReference<Activity> wr = acts.get(i);
            Activity act = wr.get();
            if (act == null || act.isFinishing()) {
                acts.remove(wr);
                i--;
            }
        }

        Activity value = null;

        // 检查oldAct是否重复
        if (acts.size() > 0) {
            value = acts.get(acts.size() - 1).get();
            if (value != null && value.hashCode() == oldAct.hashCode()) {
                startNextActivityForResult(intent, requestCode,oldAct);
                return;
            }
        }
        // 结束掉过多的act
        if (acts.size() >= 2) {
            WeakReference<Activity> one = acts.remove(0);
            if (one != null) {
                value = one.get();
                if (value != null) {
                    value.finish();
                }
            }
        }
        acts.add(new WeakReference<Activity>(oldAct));
      startNextActivityForResult(intent, requestCode,oldAct);
    }
    public void startNextActivity(Intent intent, Activity context) {

        context.startActivity(intent);
        context.overridePendingTransition(R.anim.push_right_in, R.anim.push_left_out);
    }
    public void startBottomNextActivity(Intent intent, Activity context) {

        context.startActivity(intent);
        context.overridePendingTransition(R.anim.push_bottom_out, R.anim.push_left_out);
    }
    public void startBottomNextActivityForResult(Intent intent, int code, Activity context) {
        context.startActivityForResult(intent, code);
        context.overridePendingTransition(R.anim.push_bottom_out, R.anim.push_left_out);
    }
    public void startNextActivityForResult(Intent intent, int code, Activity context) {
        context.startActivityForResult(intent, code);
        context.overridePendingTransition(R.anim.push_right_in, R.anim.push_left_out);
    }

    /**
     * 退出Activity
     * 
     * @param activity Activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null) {
            activity.finish();
            activity.overridePendingTransition(R.anim.push_left_in, R.anim.push_right_out);
        }
    }
    /**
     * 退出Activity
     * 
     * @param activity Activity
     */
    public void bottomFinishActivity(Activity activity) {
        if (activity != null) {
            activity.finish();
            activity.overridePendingTransition(R.anim.push_left_in, R.anim.push_bottom_in);
        }
    }
}
